package server.controller;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;

import server.BroadcastUpdates;
import server.Server;
import server.model.Object3D;

public class ServerObject3DControllerImpl extends UnicastRemoteObject implements ServerObject3DController{

	private static final long serialVersionUID = -5342011104546749194L;
	
	// Property
	Object3D model;
	
	// Constructor
	public ServerObject3DControllerImpl(Object3D o) throws RemoteException{
		model = o;
		try {
			Naming.rebind("rmi://"+Server.IPAdress+":"+Server.port+"/"+Server.serverName+"/"+model.getName(), this);
		} catch (Exception e) {
			System.out.println("Trouble: " + e);
		}
	}
	
	// Accessors
	public String getFileLoaded() throws RemoteException {
		return model.getFileLoaded();
	}

	public String getName() throws RemoteException {
		return model.getName();
	}

	public Quat4d getOrientation() throws RemoteException {
		return model.getOrientation();
	}

	public Vector3d getPosition() throws RemoteException {
		return model.getPosition();
	}

	// Mutators
	public void setOrientation(Quat4d q) throws RemoteException {
		model.setOrientation(q);
		BroadcastUpdates.getSharedInstance().diffuseMessage(model.getName(), model.getPosition(), model.getOrientation(), model.getFileLoaded());
	}

	public void setPosition(Vector3d v) throws RemoteException {
		model.setPosition(v);
		BroadcastUpdates.getSharedInstance().diffuseMessage(model.getName(), model.getPosition(), model.getOrientation(), model.getFileLoaded());
	}
	
	public String toString() {
		String result = "Name: " + model.getName();
		result += "Position: " + model.getPosition();
		result += "Orientation: " + model.getOrientation();
		result += "File: " + model.getFileLoaded();
		return result;
	}
}
